//
//  HNCreditGoldTargetDetailTableView.m
//  XDEducation
//
//  Created by 星道 on 17/5/31.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNCreditGoldTargetDetailTableView.h"
#import "HNCreditGoldTargetDetailCell.h"
#import "HNCreditGoldTargetDetailModel.h"
#import "HNCreditGoldBigTargetDetailModel.h"

@interface HNCreditGoldTargetDetailTableView ()
@property (nonatomic, strong) NSMutableArray *Data_Arr;
@property (nonatomic, assign) NSInteger Index;
@property (nonatomic, strong) NSNumber *AppealNum;
@end

@implementation HNCreditGoldTargetDetailTableView

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.Index = 1;
    
    
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    
    [self.tableView registerNib:[UINib nibWithNibName:[HNCreditGoldTargetDetailCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 300;
    
    [self GetAppealNum];
}




/** 获取申诉数据 */
- (void)GetAppealNum
{
    NSLog(@"%@,%@",self.executeUid,[AccountTool shareAccountTool].currentAccount.uid);
    if ([self.executeUid isEqualToString:[AccountTool shareAccountTool].currentAccount.uid])
    {
        [MBProgressHUD showHUD];
        [XDHttpTool get:BaseURL(@"user.do?action=getAppealNum") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:@{@"uid":[AccountTool shareAccountTool].currentAccount.uid} success:^(id responseObj) {
            
            if ([responseObj[@"code"] isEqualToNumber:@1000])
            {
                self.AppealNum = responseObj[@"t"];
                if (self.HNCreditGoldTargetDetailType == XiaoMuBiao)
                {
                    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                        [self BeginResh];
                    }];
                    [self.tableView.mj_footer beginRefreshing];
                }
                else
                {
                    [MBProgressHUD showHUD];
                    [self BeginResh];
                }
            }
            else
            {
                [MBProgressHUD showError:responseObj[@"msg"]];
            }
            
        } failure:^(NSError *error) {
            [MBProgressHUD showError:XDNetWorkError];
        }];
    }
    else
    {
        if (self.HNCreditGoldTargetDetailType == XiaoMuBiao)
        {
            self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self BeginResh];
            }];
            [self.tableView.mj_footer beginRefreshing];
        }
        else
        {
            [MBProgressHUD showHUD];
            [self BeginResh];
        }
    }
}



/** 获取主获取 */
- (void)BeginResh
{
    [self GetUrlStrAndParamsComplete:^(NSString *UrlStr, NSDictionary *Params) {
        [self RefreshDataWithUrlStr:UrlStr Params:Params Complete:^(id Obj) {
            [self.tableView.mj_footer endRefreshing];
            if (self.HNCreditGoldTargetDetailType == XiaoMuBiao)
            {
                if ([Obj[@"dataList"] count] == 0)
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    });
                }
                else
                {
                    for (NSDictionary *Dict in Obj[@"dataList"])
                    {
                        HNCreditGoldTargetDetailModel *Model =[HNCreditGoldTargetDetailModel mj_objectWithKeyValues:Dict];
                        Model.AppealNum = self.AppealNum;
                        [self.Data_Arr addObject:Model];
                    }
                    [self.tableView reloadData];
                    
                    self.Index += 1;
                }
            }
            else if (self.HNCreditGoldTargetDetailType == DaMuBiao)
            {
                if ([Obj[@"bigtargets"] count] == 0)
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    });
                }
                else
                {
                    for (NSDictionary *Dict in Obj[@"bigtargets"])
                    {
                        HNCreditGoldBigTargetDetailModel *Model = [HNCreditGoldBigTargetDetailModel mj_objectWithKeyValues:Dict];
                        Model.AppealNum = self.AppealNum;
                        [self.Data_Arr addObject:Model];
                    }
                    [self.tableView reloadData];
                    self.Index += 1;
                }
            }
        }];
    }];
}






- (void)RefreshDataWithUrlStr:(NSString *)UrlStr Params:(NSDictionary *)Params Complete:(void(^)(id Obj))Complete
{
    [XDHttpTool get:BaseURL(UrlStr) authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Params success:^(id responseObj) {
        Complete (responseObj);
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}






#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.Data_Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNCreditGoldTargetDetailCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (self.HNCreditGoldTargetDetailType == XiaoMuBiao)
    {
        HNCreditGoldTargetDetailModel *Model = self.Data_Arr[indexPath.row];
        Cell.Model = Model;
    }
    else if (self.HNCreditGoldTargetDetailType == DaMuBiao)
    {
        HNCreditGoldBigTargetDetailModel *Model = self.Data_Arr[indexPath.row];
        Cell.BigModel = Model;
    }
    return Cell;
}

#pragma mark - Setter
- (NSMutableArray *)Data_Arr
{
    if (!_Data_Arr)
    {
        _Data_Arr = [NSMutableArray array];
    }
    return _Data_Arr;
}



#pragma mark - 小目标
/** 老师2 家长3 孩子4 来判断帐户角色，再根据Type判断点了什么身份进来，再分别获取所有的参数*/
/** 不同的角色与身份调用接口数量不同，最多同时调用两个接口 */

- (void)GetUrlStrAndParamsComplete:(void(^)(NSString *UrlStr,NSDictionary *Params))Complete
{
    if (self.HNCreditGoldTargetDetailType == XiaoMuBiao)
    {
        //帐户角色
        NSInteger RoleCode = [[AccountTool shareAccountTool].currentAccount.rolecode integerValue];
        if (RoleCode == 2)
        {
            //老师角色没有家长孩子
            if (self.HNCreditGoldTargetDetailAccoutType == XueSheng)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":self.executeUid,@"page":@(self.Index)});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == ZiJi)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"page":@(self.Index)});
            }
            else
            {
            }
        }
        else if (RoleCode == 3)
        {
            if (self.HNCreditGoldTargetDetailAccoutType == HaiZi)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":self.executeUid,@"page":@(self.Index)});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == ZiJi)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"page":@(self.Index)});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == LaoShi)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":self.executeUid,@"executeUid":self.ChildID,@"page":@(self.Index)});
            }
            else
            {
            }
        }
        else if (RoleCode == 4)
        {
            
            if (self.HNCreditGoldTargetDetailAccoutType == JiaZhang)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":self.executeUid,@"executeUid":[AccountTool shareAccountTool].currentAccount.uid,@"page":@(self.Index)});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == LaoShi)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"supervisorUid":self.executeUid,@"executeUid":[AccountTool shareAccountTool].currentAccount.uid,@"page":@(self.Index)});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == ZiJi)
            {
                Complete (@"user.do?action=getMySmallTargetHistroy2",@{@"executeUid":[AccountTool shareAccountTool].currentAccount.uid,@"page":@(self.Index)});
            }
        }
        else
        {
        }
    }
    else if (self.HNCreditGoldTargetDetailType == DaMuBiao)
    {
        //帐户角色
        NSInteger RoleCode = [[AccountTool shareAccountTool].currentAccount.rolecode integerValue];
        if (RoleCode == 2)
        {
            //老师角色没有家长孩子
            if (self.HNCreditGoldTargetDetailAccoutType == XueSheng)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":self.executeUid});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == ZiJi)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid});
            }
            else
            {
            }
        }
        else if (RoleCode == 3)
        {
            if (self.HNCreditGoldTargetDetailAccoutType == HaiZi)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":self.executeUid});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == ZiJi)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == LaoShi)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"supervisorUid":[AccountTool shareAccountTool].currentAccount.uid,@"executeUid":self.executeUid});
            }
            else
            {
            }
        }
        else if (RoleCode == 4)
        {
            
            if (self.HNCreditGoldTargetDetailAccoutType == JiaZhang)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"supervisorUid":self.executeUid,@"executeUid":[AccountTool shareAccountTool].currentAccount.uid});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == LaoShi)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid,@"supervisorUid":self.executeUid,@"executeUid":[AccountTool shareAccountTool].currentAccount.uid});
            }
            else if (self.HNCreditGoldTargetDetailAccoutType == ZiJi)
            {
                Complete (@"user.do?action=getMyBigTarget2",@{@"uid":[AccountTool shareAccountTool].currentAccount.uid});
            }
        }
        else
        {
        }
    }
    
    
}


@end
